<template>
  <div>
    <el-dialog :title="types=='add'?'新增':'修改'" :visible="isShow" :before-close="close" width="50%">
      <!-- 这个地方填写表格 内容有啥填写啥 id不用填写 pid title url status type  提交按钮当新增的时候调用menuadd接口当修改的时候调用menudit接口 -->
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="活动区域" prop="pid">
          <el-select v-model="ruleForm.pid" placeholder="请选择活动区域">
            <el-option label="顶级目录" :value="0"></el-option>
            <el-option :label="menu.title" :value="menu.id" v-for="(menu,index) in winList" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="ruleForm.type">
            <el-radio :label="1">目录</el-radio>
            <el-radio :label="2">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="图标" v-show="ruleForm.type == 1" prop="icon">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="地址" v-show="ruleForm.type == 2" prop="url">
          <el-input v-model="ruleForm.url"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button @click="submit" type="primary">
          确定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addMenu, ditMenu } from '@/api/index'
export default {
  name: 'MyWin',
  props: ['isShow', 'winList', 'types'],
  data () {
    return {
      ruleForm: {
        pid: 0,
        title: '',
        icon: '',
        url: '',
        type: 1,
        status: '1'
      },
      rules: {
        name: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      }
    }
  },
  methods: {
    close () {
      this.$emit('updates')
    },
    submit () {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          let res
          // eslint-disable-next-line
          if (this.types == 'add') {
            res = await addMenu(this.ruleForm)
            console.log(res)
          } else {
            res = await ditMenu(this.ruleForm)
          }
          // eslint-disable-next-line
          if (res.code == 200) {
            this.$emit('update')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
      // 调用关闭函数
      this.close()
    },
    setVal (data) {
      // 给表单项赋值
      this.ruleForm = data
    }
  }
}
</script>

<style lang="less" scoped>
</style>
